﻿Ext.define('GW.view.BibleContent', {
	extend: 'Ext.panel.Panel',
	alias: 'widget.biblecontent',
	title: 'Bible content',
	requires: ['GW.view.VersePanel', 'GW.view.Search', 'Ext.util.Cookies'],
	layout: 'border',
	tools:[{
        xtype: 'button',
        text   : 'Login',
        iconCls: 'x-login',
        handler: function(){
        	var me = this;
            if (!this.loginWin) {
                var loginForm = 
            	{
            		xtype: 'form',
            		layout: {
            			type: 'vbox',
            			align: 'stretch'
            		},
            		border: false,
            		bodyPadding: 10,

                    fieldDefaults: {
                        labelAlign: 'left',
                        labelWidth: 70,
                        labelStyle: 'font-weight:bold'
                    },

                    items: [{
                    	xtype: 'textfield',
                        name: 'x_username',
                        fieldLabel: 'Username',
                        allowBlank: false
                    }, {
                        xtype: 'textfield',
                        name: 'x_password',
                        fieldLabel: 'Password',
                        inputType: 'password',
                        allowBlank: false
                    },{
                        xtype: 'hiddenfield',
                        name: 'sling:authRequestLogin',
                        value: 'ajaxauth'
                    }],

                    buttons: [{
                        text: 'Cancel',
                        handler: function() {
                            this.up('form').getForm().reset();
                            this.up('window').hide();
                        }
                    }, {
                        text: 'Send',
                        handler: function() {
                            if (this.up('form').getForm().isValid()) {
                            	var f = this.up('form').getForm();
                                f.submit({
                                    clientValidation: true,
                                    url: '/content/bibles.json',
                                    method: 'GET',
                                    success: function(form, action){
                                    	Ext.Msg.alert('Login ok. Welcome '+action+'.');
                                    },
                                    failure: function(form, action) {
                                    	Ext.Msg.alert('Login failed. Please try again.');
                                    }
                                });
                            }
                        }
                    }]
                };

                this.loginWin = Ext.widget('window', {
	                title: 'Login',
	                closeAction: 'hide',
	                layout: 'fit',
	                modal: true,
            		width: 300,
                    height: 140,
	                resizable: false,
	                items: loginForm
	            });
            }
            
            this.loginWin.show();
        }
    }],
	items: [
		{
			xtype: 'panel',
			autoScroll: true,
			region: 'center',
			bodyPadding: 5,
			itemId: 'bibleView'
		},
		{
			xtype: 'panel',
			region: 'east',
			split: true,
			minWidth: 200,
			width: 200,
			layout: 'accordion',
			align: 'stretch',
			defaults: {
		        // applied to each contained panel
				autoScroll: true,
				bodyPadding: 5
		    },
			items:[
				{
					xtype: 'search',
					title: 'Search'
				},
				{
					xtype: 'versepanel',
					title: 'References',
					itemId: 'references'
				},
				{
					xtype: 'panel',
					title: 'Notes'
				}
			]
		}
	]
});